package com.zhangyongyi.carhome.fragment.recommend.newest;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.zhangyongyi.carhome.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/11.
 * 轮播图
 */
public class FocusingFragment extends Fragment{
    private ImageView iv;
    private String imgUrl;
    private RequestQueue queue;

    private static FocusingFragment focusingFragment;
    public FocusingFragment(){
        super();
    }
    public static FocusingFragment getInstance(String imgUrl){
        FocusingFragment focusingFragment = new FocusingFragment();
        // 参数传递
        Bundle args = new Bundle();
        args.putString("imgUrl", imgUrl);
        focusingFragment.setArguments(args);

        return focusingFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        queue = Volley.newRequestQueue(context);

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            imgUrl = bundle.getString("imgUrl");

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_newes_view_pager, null);
        iv = (ImageView) v.findViewById(R.id.iv_vp_newes);
        getBitmap(imgUrl,iv);
        return v;
    }
    public void getBitmap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {

            @Override
            public void onResponse(Bitmap response) {
                // 将返回的bitmap图片对象设置到图片组件中
                imageView.setImageBitmap(response);
            }
        }
                , 720, 200, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        })
        {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        }
                ;
        queue.add(request); // 将本次图片请求加入到请求队列中
    }
}
